import getopt
import sys
import traceback
import subprocess

from Doc4 import *
from Rsyncer import *
from Util import *

class QueueMaker(Doc4):

    def __init__(self):
        Doc4.__init__(self)
        self.logpath = self.config.get("queue", "logfile")
        self.indexDir = self.config.get("queue", "indexdir")
        self.create_queue = self.config.get("queue", "createqueue")
        self.rsync_exclude = self.config.get("queue", "rsyncexclude")
        self.load_snapshot = self.config.get("queue", "loadsnapshot")

    def run(self):
        for repository_id in self.repository_ids:
            #self.clean_queue(repository)
            repository = self.get_repository(repository_id)
            if repository is not None:
                self.load_repository_contents(repository)
                self.compute_queue(repository)
            else:
                self.log("No repository data found for id %s" % repository_id)
            
    def get_next_id(self):
        result = self.execute_sql("select max(id_snapshot) as max from d4_snapshot")
        for row in result:
            max = row['max']
            if max is None:
                max = 1
            else:
                max = max + 1
        return max
    
    def load_repository_contents(self, repository):
        self.log("Loading snapshot for repository %s..." % repository)
        rsyncer = Rsyncer()
        now = time.strftime(self.date_format)
        snapshot_id = self.get_next_id()
        query = "insert into d4_snapshot (id_snapshot, id_repository, snapshot_time) values (%d, %d, '%s')" % (snapshot_id, repository.id_repository, now)
        self.execute_sql(query)
        files = rsyncer.get_repository_file_list(repository)
        for file in files:
            insertquery = "insert into d4_queue (id_snapshot, file, action, action_state) values ('%d', '%s', 'extraction','queued')" % (snapshot_id, file)
            out = self.execute_sql(insertquery)
            
        self.log("Loading finished")
            
    def clean_repository_snapshot(self, distrib):
        self.log("Cleaning snapshot table for distribution %s..." % (distrib))
        cleansnapshot = self.config.get("queue", "cleansnapshot")
        result = self.execute_sql(cleansnapshot, distrib)
        
    def compute_queue(self, repository):
        self.log("Computing queue for repository %s" % repository)    
        query = "update d4_queue set action_state='done' where exists (select file from d4_archive where d4_archive.file=d4_queue.file)"
        result = self.execute_sql(query)
        self.compute_rsync_queue(repository)

    def compute_rsync_queue(self, repository):
        #rsyncexclude = "select distinct(package) as pkg from SNAPSHOT_DISTRIBUTION_PACKAGES SDP where not exists (select package from PACKAGE P where P.package=SDP.package)"
        now = time.strftime(self.date_format)
        query = "select distinct(file) from d4_archive A, d4_snapshot S, d4_repository R where A.id_snapshot=S.id_snapshot and S.id_repository=R.id_repository and R.id_repository=%d" % repository.id_repository
        result = self.execute_sql(query)
        exclude_file_path = os.path.join("/home/doc4/mirror/excludes/", "repository-%d.exclude" % repository.id_repository)
        set_text("", exclude_file_path)
        for row in result:
            write_string("**/"+row['file']+"\n", exclude_file_path, False)
            
def main():
    qmaker = QueueMaker()
    qmaker.run()

if __name__ == "__main__":
    main()
